Skip to content

Resource monitor: divide heap utilization by the container memory limit - #7991

Open
afhassan wants to merge 4 commits into
cortexproject:masterfrom
afhassan:afayek/resource-monitor-container-heap-limit
Open

afhassan wants to merge 4 commits into
cortexproject:masterfrom
afhassan:afayek/resource-monitor-container-heap-limit

Conversation

@afhassan

@afhassan afhassan commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What this PR does:

The resource monitor divides heap objects by GOMEMLIMIT, so a heap rejection threshold is a fraction of the GC's own target. When GOMEMLIMIT drives the GC, the heap goal is (GOMEMLIMIT - non-heap) x 0.97, about 0.93 of GOMEMLIMIT, and heap objects climb to that goal before every collection. A heap_utilization: 0.9 threshold therefore rejects queries before each GC cycle can free its garbage. The threshold cannot be moved above the GC goal without also moving the goal, because both scale with GOMEMLIMIT.

This PR adds the experimental -resource-monitor.heap-limit (resource_monitor.heap_limit):

  • gomemlimit (default): divide by GOMEMLIMIT. No behaviour change.
  • container: divide by the container memory limit, read from cgroups.

With container, heap thresholds are a fraction of the container memory limit, so GOMEMLIMIT and the rejection line can be set independently: GC goal < rejection line < container limit. The setting applies to every user of the resource monitor's heap value: ingester and store-gateway rejection, and querier eviction.

If container is set and the limit cannot be read (no memory limit, not in a cgroup), the monitor logs a warning and reports heap utilization 0. Heap-based rejection and eviction stay off rather than acting on the wrong limit, and the process still starts. CPU-based limits are unaffected.

New dependency: github.com/KimMachineGun/automemlimit v1.0.2 (MIT), for memlimit.FromCgroup(). It handles cgroup v1, v2 and nested hierarchies through /proc/self/mountinfo and /proc/self/cgroup, so Cortex does not carry its own cgroup parser. Prometheus uses the same library and version to derive GOMEMLIMIT from the container memory limit (--auto-gomemlimit, on by default): see the import in cmd/prometheus/main.go and go.mod. It brings in github.com/pbnjay/memory (BSD-3-Clause), also a Prometheus dependency. Both are vendored.

Which issue(s) this PR fixes:
None

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags
  • I did not look at code from AGPL licensed projects to make this PR

The resource monitor divides heap objects by GOMEMLIMIT, so a heap
rejection threshold is a fraction of the GC's own target. When GOMEMLIMIT
drives the GC, the heap goal is (GOMEMLIMIT - non-heap) x 0.97, about 0.93
of GOMEMLIMIT, and heap objects climb to it before every collection. A 0.9
threshold therefore rejects queries before each GC cycle can free its
garbage, and the threshold cannot be moved above the GC goal without also
moving the GC goal, because both scale with GOMEMLIMIT.

Add -resource-monitor.heap-limit with two values:
- gomemlimit (default): divide by GOMEMLIMIT, as today.
- container: divide by the container memory limit, read from cgroups with
  github.com/KimMachineGun/automemlimit (memlimit.FromCgroup), which
  handles cgroup v1, v2 and nested hierarchies.

With container, heap_utilization thresholds are a fraction of pod memory,
so GOMEMLIMIT (where the GC runs) and the rejection threshold can be set
independently, e.g. GC goal < threshold < pod limit. If the container
memory limit cannot be read, the monitor logs a warning and reports a heap
utilization of 0, so heap-based rejection and eviction stay off rather than
acting on the wrong limit, and the pod still starts. CPU-based limits are
unaffected.

Signed-off-by: Ahmed Hassan <afayek@amazon.com>
Signed-off-by: Ahmed Hassan <afayek@amazon.com>
Signed-off-by: Ahmed Hassan <afayek@amazon.com>
Signed-off-by: Ahmed Hassan <afayek@amazon.com>
@afhassan
afhassan marked this pull request as ready for review December 4, 2026 18:41
@afhassan
afhassan requested a review from a team as a code owner December 4, 2026 18:41
@afhassan
afhassan requested a review from alanprot December 4, 2026 18:41

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant